<?php

class ReportsController extends AppController {

	var $uses = array ('Item','District','Location' );
	var $name = 'Reports';
	var $header = 'Reports';
	var $helpers = array('Excel');
	
	private $report_rows;
	function beforeFilter() {
		parent::beforeFilter ();
	}
	function generateMonthly($month = null) {
		$date = new DateTime ();
		if ($month === null) {
		}
	}

	function generateRange($start_date, $end_date = null) {
		prd ( "calling range" );
	}

	function year($mode = null, $val = null) {
		$dt = new DateTime ();
		$year = $dt->format ( 'Y' );
		if (! empty ( $val )) {
			$year = $val;
		}
		switch ($mode) {
			case "sale" :
				$this->loadModel ( 'Sale' );
				$sales = $this->Store->Sale->find ();
			case "buy" :
				//generate buy for current year
				break;
			default :
				//generate both for current year
				break;
		}
	}

	function index() {
		
		if(isset($this->data)) {
			$post = $this->data['Report'];
			$dtfrom = date("Y-m-d", strtotime($post['datefrom']));
			$dtto = date("Y-m-d", strtotime($post['dateto']));
			$district_id = $post['district_id'];
			$allDistricts = true;
				
			$conditions = array('Location.date >='=>$dtfrom,
								'Location.date <='=>$dtto,
								'Location.active'=>1,
			);
				
			if(!empty($district_id)) {
				$conditions['Location.district_id'] = $district_id;
				$allDistricts = false;
			}
				
			if(!$allDistricts) {
				$districts = $this->Location->District->findAllById($district_id);
			}else {
				$this->Location->District->recursive = -1;
				$districts = $this->Location->District->find('all');
			}
			
			$categories = $this->Item->Category->find('list');
			foreach($districts as $district) {
				foreach($categories as $cat_id=>$cat_name) {
					$conditions['Item.category_id'] = $cat_id;
					$conditions['Location.district_id'] = $district['District']['id'];
					$types = $this->Item->Type->find('list',array('conditions'=>array('Type.category_id'=>$cat_id)));
					foreach($types as $type_id=>$type_name) {
						$conditions['Item.type_id'] = $type_id;
						$count = $this->Location->find('count',array('conditions'=>$conditions));
						if($count >	 0) {
							$rows[$district['District']['name']][$cat_name][$type_name] = $count;
						}
					}
				}
			}
			$this->Session->write('rows',$rows);
			$this->set('datefrom',$post['datefrom']);
			$this->set('dateto',$post['dateto']);
			$this->set(compact('rows'));
		}

		$contingents = $this->District->Contingent->find('list');
		$districts = $this->District->find('list');
		$categories = $this->Item->Category->find('list');
		$types = $this->Item->Type->find('list');
		$this->set(compact(array('districts','contingents','categories','types')));

	}
	
	function disposal() {
		
		if(isset($this->data)) {
			$post = $this->data['Report'];
			$dtfrom = date("Y-m-d", strtotime($post['datefrom']));
			$dtto = date("Y-m-d", strtotime($post['dateto']));
			$district_id = $post['district_id'];
			$allDistricts = true;
				
			$conditions = array('Disposal.disposal_date >='=>$dtfrom,
								'Disposal.disposal_date <='=>$dtto,
								'Disposal.details'=>'lupus',
			);
				
			if(!empty($disposal_id)) {
				$conditions['Disposal.item_id'] = $disposal_id;
				//$allDistricts = false;
			}

			/*
			if(!$allDistricts) {
				$districts = $this->Location->District->findAllById($district_id);
			}else {
				$this->Location->District->recursive = -1;
				$districts = $this->Location->District->find('all');
			}*/
			
			$categories = $this->Item->Disposal->find('list');
			foreach($districts as $district) {
				foreach($categories as $cat_id=>$cat_name) {
					$conditions['Item.category_id'] = $cat_id;
					$conditions['Location.district_id'] = $district['District']['id'];
					$types = $this->Item->Type->find('list',array('conditions'=>array('Type.category_id'=>$cat_id)));
					foreach($types as $type_id=>$type_name) {
						$conditions['Item.type_id'] = $type_id;
						$count = $this->Location->find('count',array('conditions'=>$conditions));
						if($count >	 0) {
							$rows[$district['District']['name']][$cat_name][$type_name] = $count;
						}
					}
				}
			}
			$this->Session->write('rows',$rows);
			$this->set('datefrom',$post['datefrom']);
			$this->set('dateto',$post['dateto']);
			$this->set(compact('rows'));
		}

		$contingents = $this->District->Contingent->find('list');
		$districts = $this->District->find('list');
		$categories = $this->Item->Category->find('list');
		$types = $this->Item->Type->find('list');
		$this->set(compact(array('districts','contingents','categories','types')));

	}
	
	function exportPdf() {
		Configure::write('debug',0);
		$this->set('rows',$this->Session->read('rows'));
		$this->layout = 'pdf'; //this will use the pdf.ctp layout 
        $this->render(); 
	}
	
	function exportXls() {
		Configure::write('debug',0);
		$this->layout = 'ajax'; 
        $this->set('rows',  $this->Location->District->find('all'));
	}
	
	function _getRows() {
		
	}
}